package com.user.test.model.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [SearchResult::class], version = 1)
abstract class SearchResultDatabase : RoomDatabase() {

    abstract fun searchResultDao(): SearchResultDao

    companion object {
        @Volatile
        private var INSTANCE: SearchResultDatabase? = null

        @Synchronized
        fun getInstance(context: Context): SearchResultDatabase{
            //val executorServ = Executors.newSingleThreadExecutor()
            if(INSTANCE == null){
                INSTANCE = Room.databaseBuilder(context.applicationContext, SearchResultDatabase::class.java, "search_db")
                    /*.addCallback(object : RoomDatabase.Callback(){
                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        executorServ.execute(Runnable {
                            getInstance(context).searchResultDao().insert(SearchResult(0, "Title", 0, "https://google.com"))
                        })
                        executorServ.shutdown()
                    }
                })*/
                    .build()
            }
            return INSTANCE as SearchResultDatabase
        }
    }
}
